<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Page</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background-color: #202123;
            /* Dark mode background */
            color: #FFFFFF;
            /* Text color */
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            text-align: center;
            background-color: #33373E;
            /* Slightly lighter background */
            padding: 40px 50px;
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
            width: 100%;
            max-width: 400px;
        }

        h1 {
            margin-bottom: 20px;
            font-size: 24px;
        }

        form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        input {
            background-color: #202123;
            /* Input background */
            color: #FFFFFF;
            /* Input text */
            border: 1px solid #444951;
            /* Subtle border */
            padding: 10px 15px;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        input:focus {
            border-color: #10A37F;
            /* Highlight border on focus */
            outline: none;
        }

        button {
            background-color: #10A37F;
            /* Green accent */
            color: #FFFFFF;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0E8F6E;
            /* Darker green on hover */
        }

        .footer {
            margin-top: 20px;
            font-size: 14px;
            color: #B0B3B8;
            /* Muted text */
        }

        .footer a {
            color: #10A37F;
            text-decoration: none;
            transition: color 0.3s;
        }

        .footer a:hover {
            color: #0E8F6E;
        }

        .message {
            color: green;
            margin-top: 20px;
            display: none;
        }

        .error-message {
            color: red;
            margin-top: 20px;
            display: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Login to Your Account</h1>
        <form id="loginForm">
            <input type="text" id="username" placeholder="Username" required>
            <input type="password" id="password" placeholder="Password" required>
            <button type="submit">Login</button>
        </form>
        <div class="footer">
            <p>Don't have an account? <a href="/register">Sign up</a></p>
            <p>Forgot your password? <a href="/forgot-password">Reset it</a></p>
        </div>
        <div id="errorMessage" class="error-message"></div>
        <div id="successMessage" class="message"></div>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function (event) {
            event.preventDefault(); // 阻止表单的默认提交行为

            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;

            var data = {
                username: username,
                password: password
            };

            fetch('login.lua', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })
                .then(response => {
                    if (!response.ok) {
                        return response.json().then(data => {
                            throw new Error(data.message || 'Network response was not ok');
                        });
                    }
                    return response.json();
                })
                .then(data => {
                    if (data.success) {
                        localStorage.setItem('username', username); // 存储用户名到 localStorage
                        document.getElementById('username').value = '';
                        document.getElementById('password').value = '';
                        document.getElementById('errorMessage').innerText = '';
                        window.location.href = '/welcome'; // 登录成功，跳转到欢迎页面
                    } else {
                        document.getElementById('errorMessage').innerText = data.message || "Login failed. Please try again.";
                        document.getElementById('errorMessage').style.display = 'block';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    document.getElementById('errorMessage').innerText = error.message || "An unknown error occurred. Please try again.";
                    document.getElementById('errorMessage').style.display = 'block';
                });
        });
    </script>
</body>

</html>